//@ts-ignore
import {useConfirmDialog} from "/src/hooks/DialogMsg.ts"


/**
 * 检测系统更新
 * @param timeout 检测间隔,时间单位毫秒
 */
export async function checkSystemUpdate(timeout: number = 5000) {
    const result = await fetch(location.pathname + '?v=' + (new Date()).getTime())
    if (result.status === 200) {
        const newHtml = await result.text()
        const oldHtml = document.documentElement.outerHTML
        const regex = /zms-tag="([^"]*)"/;
        let nv: string, ov: string = '';
        const nMatch = regex.exec(newHtml);
        if (nMatch && nMatch[1]) {
            nv = nMatch[1]
        }
        const oMatch = regex.exec(oldHtml);
        if (oMatch && oMatch[1]) {
            ov = oMatch[1]
        }
        if (nv === ov) {
            setTimeout(checkSystemUpdate, timeout)
        } else {
            useConfirmDialog('检测到系统已更新,请刷新页面更新', '系统更新', {
                showCancelButton: false,
                type: "success"
            }).then(() => {
                location.reload()
            })
        }
    } else {
        setTimeout(checkSystemUpdate, timeout)
    }
}